/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package quangtqServlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import quangtq.MailPagingRemote;

/**
 *
 * @author QUANG2PRO
 */
@WebServlet(name="CountPageServlet", urlPatterns={"/CountPageServlet"})
public class CountPageServlet extends HttpServlet {
    @EJB
    private MailPagingRemote mailPaging;
   
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
        String isInbox = req.getParameter("isInbox");
        String userName = (String)req.getSession().getAttribute("username");
        int numOfMail = mailPaging.numberOfMails(userName, isInbox.equals("1") ? true : false);
        int numOfPage;

        if(numOfMail % 10 == 0)
        {
            numOfPage = numOfMail / 10;
        }
        else
        {
            numOfPage = numOfMail / 10 + 1;
        }

        res.setContentType("text/xml");
        res.setHeader("Cache-Control", "no-cache");
        // write out the response string
        res.getWriter( ).write(String.valueOf(numOfPage));
    }
}
